package zozopo.flow.comm;

import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import zozopo.background.operator.operations.OperatorTB;
import zozopo.background.operator.operations.OperatorlimitedTB;
import zozopo.database.BusiDatabaseManager;
import zozopo.flow.operations.FlowOS;
import zozopo.flow.operations.FlowTB;
import zozopo.flow.operations.RecordlistOS;
import zozopo.flow.operations.RecordlistTB;
import zozopo.operations.comm.OperatingService;
import zozopo.user.operations.UserTB;
import zozopo.warehouse.operations.PurchaselistOS;
import zozopo.warehouse.operations.PurchaselistTB;
import zozopo.warehouse.operations.ReceiptOS;
import zozopo.warehouse.operations.ReceiptlistOS;
import zozopo.warehouse.operations.ReceiptlistTB;

public class Flow extends OperatingService{
	
	private static final Log logger = LogFactory.getLog(Flow.class);
	
	private FlowOS flowOS;
	private RecordlistOS recordlistOS;
	
	public Flow(Connection conn){
		super(conn);
		flowOS = new FlowOS(conn);
		recordlistOS = new RecordlistOS(conn);
	}
	
	public Flow(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
		flowOS = new FlowOS(this.conn);
		recordlistOS = new RecordlistOS(this.conn);
		
	}
	
	public boolean executeFlow(String kind, int kindid,String nextCode,OperatorTB operator,Map<String,OperatorlimitedTB> userlimitedMap,String remark,double doublevalue,int intvalue) {
		
		
		if(nextCode != null && userlimitedMap.get(nextCode) == null){
			return false;
		}
		
		Map<Integer,List<FlowTB>> flowMap = flowOS.getFlowValueMapByKind(kind);
		
		RecordlistTB recordlist = new RecordlistTB();
		
		try { 
			if(!recordlistOS.isExist(kind, kindid)){
				FlowTB flow = flowMap.get(0).get(0);
				
				flowOS.add(flow.getCode_flow());//累积流程状态数据
				
				recordlist.setCnname_recordlist(flow.getCnname_flow());
				recordlist.setCode_recordlist(flow.getCode_flow()+"");
				recordlist.setEnname_recordlist(flow.getEnname_flow()+"");
				recordlist.setKind_recordlist(flow.getKind_flow());
				recordlist.setKindid_recordlist(kindid);
				recordlist.setUseremail_recordlist(operator.getEmail_operator());
				recordlist.setUsername_recordlist(operator.getName_operator());
				
				recordlist.setFlowflag_recordlist("1");
				recordlist.setDoublevalue_recordlist(doublevalue);
				recordlist.setValue_recordlist(0);
				recordlist.setNextvalue_recordlist(flow.getNextvalue_flow());
				recordlist.setRemark_recordlist(remark);
				recordlistOS.insert(recordlist);
			}else{
				
				
				recordlist = recordlistOS.getNewValue(kind,kindid);
				
				if(recordlist.getNextvalue_recordlist() == -1){  //-1为流程结束 不需要再继续往下走
					return false;
				}//*/
				
				recordlistOS.upadte(recordlist.getId_recordlist(), "0");
				
				FlowTB flow = flowOS.getFlow(nextCode);
			
				
				//FLOW 状态 数据统计
				flowOS.add(flow.getCode_flow());
				flowOS.reduce(recordlist.getCode_recordlist());
				
				if(recordlist.getNextvalue_recordlist() == flow.getValue_flow()){  //如果本流程的下个流程等于此次传入的值才做以下处理
					recordlist.setCnname_recordlist(flow.getCnname_flow());
					recordlist.setCode_recordlist(flow.getCode_flow());
					recordlist.setEnname_recordlist(flow.getEnname_flow());
					recordlist.setKind_recordlist(flow.getKind_flow());
					recordlist.setKindid_recordlist(kindid);
					recordlist.setUseremail_recordlist(operator.getEmail_operator());
					recordlist.setUsername_recordlist(operator.getName_operator());
					recordlist.setRemark_recordlist(remark);
					recordlist.setFlowflag_recordlist("1");
					recordlist.setValue_recordlist(0);
					recordlist.setDoublevalue_recordlist(doublevalue);
					recordlist.setNextvalue_recordlist(flow.getNextvalue_flow());
					
					
					
					recordlistOS.insert(recordlist);
				}else{
					return false;
				}
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return true;
	}
	public boolean executeFlow(String kind, int kindid,String nextCode,OperatorTB operator,Map<String,OperatorlimitedTB> userlimitedMap,String remark,double doublevalue) {
		return executeFlow(kind,kindid,nextCode,operator,userlimitedMap,remark,doublevalue,0);
	}
	
	public boolean executeFlow(String kind, int kindid,String nextCode,OperatorTB operator,Map<String,OperatorlimitedTB> userlimitedMap,String remark) {
		return executeFlow(kind,kindid,nextCode,operator,userlimitedMap,remark,0.0,0);
	}
	
	public FlowTB getFlow(String code){
		FlowTB flow = flowOS.getFlow(code);
		return flow;
	}
	
	public List<FlowTB> getFlowListByKind(String kind){
		List<FlowTB> recordlist = flowOS.getFlowListByKind(kind);
		return recordlist;
	}
	
	
	
	public Map<Integer,List<FlowTB>> getFlowValueMapByKind(String kind){
		Map<Integer,List<FlowTB>> flowMap = flowOS.getFlowValueMapByKind(kind);
		return flowMap;
	}
	
	public Map<String,FlowTB> getFlowCodeMapByKind(String kind){
		Map<String,FlowTB> flowMap = flowOS.getFlowCodeMapByKind(kind);
		return flowMap;
	}
	
	
	public String getRecordlistState(String kind, int kindid){
		RecordlistTB recordlist = recordlistOS.getNewValue(kind,kindid);
		return recordlist.getCnname_recordlist();
	}
	
	public int getRecordlistValue(String kind, int kindid){
		RecordlistTB recordlist = recordlistOS.getNewValue(kind,kindid);
		return recordlist.getValue_recordlist();
	}
	
	public RecordlistTB getRecordlist(String kind, int kindid){
		RecordlistTB recordlist = recordlistOS.getNewValue(kind,kindid);
		return recordlist;
	}
	
	public Map<Integer,RecordlistTB> getRecordlistMapByKind(String kind){
		
		List<RecordlistTB> recordlistList = recordlistOS.select(kind);
		
		Map<Integer,RecordlistTB> recorlistMap = new HashMap<Integer,RecordlistTB>();
		
		for(RecordlistTB recordlist:recordlistList){
			recorlistMap.put(recordlist.getKindid_recordlist(), recordlist);
		}
		
		return recorlistMap;
	}
	
	
	
	public Map<String,RecordlistTB> getRecordlistCodeMap(String kind,int kindid){
		
		List<RecordlistTB> recordlistList = recordlistOS.select(kind,kindid);
		
		Map<String,RecordlistTB> recorlistMap = new HashMap<String,RecordlistTB>();
		
		for(RecordlistTB recordlist:recordlistList){
			recorlistMap.put(recordlist.getCode_recordlist(), recordlist);
		}
		
		return recorlistMap;
	}
	
	public Map<Integer,RecordlistTB> getRecordlistIdMap(String kind,int kindid){
		
		List<RecordlistTB> recordlistList = recordlistOS.select(kind,kindid);
		
		Map<Integer,RecordlistTB> recorlistMap = new HashMap<Integer,RecordlistTB>();
		
		for(RecordlistTB recordlist:recordlistList){
			recorlistMap.put(recordlist.getId_recordlist(), recordlist);
		}
		
		return recorlistMap;
	}
	
	public Map<String,RecordlistTB> getRecordlistCodeMapNoCancel(String kind,int kindid){
		
	//	recordlistOS.select(kind,kindid);
		
		String sql = "SELECT * FROM " + RecordlistTB.TABLE_NAME  
		+" where " + RecordlistTB.KIND_RECORDLIST + " ='"+kind+"'"
		+ " AND " + RecordlistTB.KINDID_RECORDLIST + " ='"+kindid+"'"
		+ " AND " + RecordlistTB.FLOWFLAG_RECORDLIST + " !='9'"
		+" ORDER BY "+RecordlistTB.ID_RECORDLIST+" DESC "; 
		
		List<RecordlistTB> recordlistList = recordlistOS.selectListBySql(sql);
		Map<String,RecordlistTB> recorlistMap = new HashMap<String,RecordlistTB>();
		
		for(RecordlistTB recordlist:recordlistList){
			recorlistMap.put(recordlist.getCode_recordlist(), recordlist);
		}
		
		return recorlistMap;
	}
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public boolean isNextCode(String kind, int kindid,String nextCode){
		RecordlistTB recordlist = new RecordlistTB();
		recordlist = recordlistOS.getNewValue(kind,kindid);
		FlowTB flow = flowOS.getFlow(nextCode);
		
		if(recordlist.getNextvalue_recordlist() == flow.getValue_flow()){  //如果本流程的下个流程等于此次传入的值才做以下处理
			return true;
		}else{
			return false;
		}	
	}
	
	
	
	/**
	 * @see 把所快递费加回出仓单
	 * @version 1.0
	 * @author Shea
	 */
	public String addAllFlowDoubleToReceiptlist(){
		
		String addInfo = "";
		//ReceiptOS receiptOS = new ReceiptOS(conn);
		ReceiptlistOS receiptlistOS = new ReceiptlistOS(conn);
		
		List<ReceiptlistTB> receiptlistList = receiptlistOS.selectAll();
		//receiptList = receiptOS.getListByReceiptlistId(maxId);
		
		//Flow flow = new Flow(conn);
		
		for(ReceiptlistTB receiptlist:receiptlistList){
		//	int xxid = receiptlist.getId_receiptlist();
			RecordlistTB recordlist = recordlistOS.getCurrentValue(FlowCode.WAREHOUSE_RECEIPTLIST, receiptlist.getId_receiptlist());
			
			if(recordlist != null ){
				
			
				if(recordlist.getCode_recordlist() != null && 
						recordlist.getCode_recordlist().equals("WAREHOUSE.RECEIPTLIST.CONFIRMSHIPPRICE")){
					receiptlistOS.upadteForProfit(receiptlist, recordlist.getDoublevalue_recordlist());
					addInfo += "加入成功 " + receiptlist.getId_receiptlist() + ",";
				}else{
					addInfo += "<font color='red'>加入错误 " + receiptlist.getId_receiptlist() + ",</font>";
					
				} 
			}
		}
		
		return addInfo;
		
	}	
	
	/**
	 * @see 把所快递费加回出仓单
	 * @version 1.0
	 * @author Shea
	 */
	public String addAllFlowToPurchaselistAction(Map<String,OperatorlimitedTB> userlimitedMap,OperatorTB operator){
		
		String addInfo = "";
		//ReceiptOS receiptOS = new ReceiptOS(conn);
		PurchaselistOS purchaselistOS = new PurchaselistOS(conn);
		
		List<PurchaselistTB> purchaselistList = purchaselistOS.select();
		//receiptList = receiptOS.getListByReceiptlistId(maxId);
		
		//Flow flow = new Flow(conn);
		
		for(PurchaselistTB receiptlist:purchaselistList){
			//加入流程
			Flow flow = new Flow(conn);
			try{
				if(flow.executeFlow(FlowCode.WAREHOUSE_PURCHASELIST, receiptlist.getId_purchaselist(),null, operator, userlimitedMap,"")){
					addInfo += "加入成功 " + receiptlist.getId_purchaselist() + ",";
				}else{
					addInfo += "<font color='red'>加入错误 " + receiptlist.getId_purchaselist() + ",</font>";
					
				} 
			}catch(Exception  e){
				
			}
		
		}
		
		return addInfo;
		
	}	
}
 